<?php

class DBconect{
	private static $conect;
	const AUTHOR_EXISTS=1;
	const AUTHOR_DONT_EXISTS=0;

	
	function __construct(){
		if(!self::$conect){
			self::$conect=mysql_connect("localhost","root","")
			or die("Could not connect: ".mysql_error());
				
			mysql_select_db("bookshare",self::$conect)
			or die("Could not find database: ".mysql_error());
		}
	}

	
//-----REGISTER a.k.a. ADD USER-----
	
	// curent date function
// 	function curentDate(){
// 		$curentdate=mysql_query("select curdate()", self::$conect)
// 					or die("Problem reading table: ".mysql_error());
// 		return $curentDate;
// 	}
		
	function addUser($user){
		$query="insert into users
				values (null, '$user->username','$user->password','getdate()')";
		$dbrecords=mysql_query($query, self::$conect)
					or die("Problem reading table users: ".mysql_error());
	}
	
	function addProfile($profile,$user){
		$query="insert into profiles
				select user_id, '$profile->getName','$profile->getEmail'
				from users
				where username='$user->getUserName'";
		$dbrecords=mysql_query($query, self::$conect)
					or die("Problem reading table profiles: ".mysql_error());
	}
	
//-----UPLOAD BOOK-----
	
	//add path function
	function addPath($bookpath){
		$query="insert into path
				values (null, '$bookpath->path')";
		$dbrecords=mysql_query($query, self::$conect)
					or die("Problem reading table path: ".mysql_error());
	}
	//add author function
	function addAuthor($author){
		$allAuthors=mysql_query('select a_name from authors', self::$conect)
					or die("Problem reading table authors: ".mysql_error());
		$numAuthors=mysql_num_rows($allAuthors);
		if ($numAuthors==0){
			$dbrecords=mysql_query("insert into authors
									values (null, '$author->a_name', null)", self::$conect)
						or die("Problem reading table authors: ".mysql_error());
		}
		else{
 			$br=0;
			for($i=0;$i<$numAuthors;$i++){
 				$name=mysql_result($allAuthors,$i,'a_name');
			 	if(strcmp($name,$author->a_name)!=0){
					$br=0;
				}
				else $br++;
			} 
			if (!$br){
				$query1="insert into authors
						 values (null, '$author->a_name', null)";
				$dbrecords1=mysql_query($query1, self::$conect)
				or die("Problem reading table authors: ".mysql_error());
			}
			
		}
	}
	
	//add book function
	function addBook($book){
		$query="insert into books
				values (null,'$book->title', '$book->description','$book->language', '$book->coverpath')";
		$dbrecords=mysql_query($query, self::$conect)
					or die("Problem reading table books: ".mysql_error());
	}
	
	//add book_author conecting function
	function addBookAuthorConect($book,$author){
		$query="insert into books_authors 
				values((select book_id
						from books 
						where title='$book->title' and description='$book->description' 
						and language='$book->language' and cover='$book->coverpath'),
					(select author_id 
						from authors 
						where a_name='$author->a_name'))";
		$dbrecords=mysql_query($query, self::$conect)
				   or die("Problem reading table books1: ".mysql_error());
	}
	
	//add book_path conecting function
	function addBookPathConect($book,$bookpath){
		$query="insert into books_path
				values((select book_id
						from books
						where title='$book->title' and description='$book->description'
						and language='$book->language' and cover='$book->coverpath'),
					(select path_id
					from path
					where path='$bookpath->path'
					limit 1))";
		$dbrecords=mysql_query($query, self::$conect)
					or die("Problem reading table books2: ".mysql_error());
	}
	
//-----QUOTES ADMIN-----
	//add quote function
	function addQuote($quote,$quote_author){
		$query="insert into quotes
				values(null,\"$quote\",\"$quote_author\")";
		$dbrecords=mysql_query($query, self::$conect)
					or die("Problem reading table quotes: ".mysql_error());
	}
	
	//show all quotes function
	function showAllQuotes(){
		$allQuotes="select quote from quotes";
		$dbQuotes=mysql_query($allQuotes, self::$conect)
					or die("Problem reading table quotes1: ".mysql_error());
		$allAuthors="select author from quotes";
		$dbAuthors=mysql_query($allAuthors, self::$conect)
					or die("Problem reading table quotes1: ".mysql_error());
		$numQuotes=mysql_num_rows($dbQuotes);
		
		for($i=0;$i<$numQuotes;$i++){
			$quote=mysql_result($dbQuotes,$i,'quote');
			$author=mysql_result($dbAuthors,$i,'author');
			echo ($i+1).". <i>'".$quote."'</i> - ".$author."<br/><br/>";
		}
	}
	//show random quote function
	function showRandomQuote(){
		$dbQuotes=mysql_query("select quote from quotes", self::$conect)
					or die("Problem reading table quotes1: ".mysql_error());
		$numQuotes=mysql_num_rows($dbQuotes); 
		$random=mt_rand(1, $numQuotes);
		$chooseQuote="select quote,author from quotes where quote_id=".$random;
		$dbQuote=mysql_query($chooseQuote, self::$conect)
					or die("Problem reading table quotes: ".mysql_error());
		$quote=mysql_result($dbQuote,0,'quote');
		$author=mysql_result($dbQuote,0,'author');
		echo '<i>"'.$quote.'</i>"<br/><p><b>'.$author.'</b><p>';
	}
	
//-----GENRES ADMIN-----	

	//add genre function
	function addGenre($genre){
		$query="insert into genre
				values(null,\"$genre\")";
		$dbrecords=mysql_query($query, self::$conect)
					or die("Problem reading table genres: ".mysql_error());
		
	}
	
	function showAllGenres(){
		$dbGenres=mysql_query("select genre_name from genre", self::$conect)
					or die("Problem reading table genre1: ".mysql_error());
		$numGenres=mysql_num_rows($dbGenres);
		for($i=0;$i<$numGenres;$i++){
			$genre=mysql_result($dbGenres,$i,'genre_name');
			echo ($i+1).". ".$genre."<br/>";
		}
	}
}
?>